package com.woniuxy.oldmanapi.controller;

import com.github.pagehelper.PageInfo;
import com.woniuxy.oldmanapi.cvo.AddDeliveryVo;
import com.woniuxy.oldmanapi.cvo.DeliveryVo;
import com.woniuxy.oldmanapi.cvo.WarehouseVo;
import com.woniuxy.oldmanapi.service.WareHouseService;
import com.woniuxy.oldmanapi.vo.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

@RestController
public class WarehouseController {
    @Autowired
    WareHouseService wareHouseService;
    //查询入库单，以及入库单明细
    @GetMapping("/warehouse/searchList")
    public ResponseResult<PageInfo<WarehouseVo>> list(String buyname,
                                                      @RequestParam(value = "state", defaultValue = "-1") int state,
                                                      @RequestParam(value = "size", defaultValue = "6") int size,
                                                      @RequestParam(value = "now", defaultValue = "1") int now) {

        ResponseResult<PageInfo<WarehouseVo>> result = new ResponseResult<>();
        try {
            PageInfo<WarehouseVo> pageInfo = wareHouseService.findWarehouseVosByCons(buyname, state, size, now);
            result.setMsg("查询成功");
            result.setCode(200);
            result.setData(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMsg("查询失败");
            result.setCode(201);

        }
        return result;
    }

    //添加==药品入库单
    @PostMapping("/warehouse/add")
    public ResponseResult<Void> addwarehouse(@RequestBody WarehouseVo warehouseVo) {

        ResponseResult<Void> result = new ResponseResult<>();
        try {
            wareHouseService.addWarehouse(warehouseVo);
            result.setMsg("添加成功");
            result.setCode(200);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMsg("添加失败");
            result.setCode(201);
        }
        return result;
    }

    //编辑==药品入库单
    @PostMapping("/warehouse/modify")
    public ResponseResult<Void> modifyWarehouse(@RequestBody WarehouseVo warehouseVo) {

        ResponseResult<Void> result = new ResponseResult<>();
        try {
            wareHouseService.modifyWarehouse(warehouseVo);
            result.setMsg("修改成功");
            result.setCode(200);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMsg("修改失败");
            result.setCode(201);
        }
        return result;
    }

    //撤销
    @GetMapping("/warehouse/cancel/{id}")
    public ResponseResult<Void> cancel(@PathVariable("id") int id) {

        ResponseResult<Void> result = new ResponseResult<>();
        try {
            wareHouseService.cancelWarehouse(id);
            result.setMsg("撤销成功");
            result.setCode(200);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMsg("撤销失败");
            result.setCode(201);

        }
        return result;
    }

    //入库
    @PostMapping("/warehouse/putstoregy")
    public ResponseResult<Void> putWarehouse(@RequestBody WarehouseVo warehouseVo) {

        ResponseResult<Void> result = new ResponseResult<>();
        try {
            wareHouseService.putWarehouse(warehouseVo);
            result.setMsg("入库成功");
            result.setCode(200);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMsg("入库失败");
            result.setCode(201);

        }
        return result;
    }

}
